﻿using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using AvaloniaEdit.CodeCompletion;
using MarkdownRow.Edit.CompletionData;

namespace MarkdownRow.EditView;

public partial class SimpleEditor : Editor
{
    public SimpleEditor(string context)
    {
        InitializeComponent();
        Init(context);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        var a = GetMain();
        if(a == null)return;
        _editor.Options.ShowTabs = a.Setting.EditorSetting.IsShowTabs;
        _editor.Options.ShowSpaces = a.Setting.EditorSetting.IsShowSpaces;
        _editor.Options.ShowEndOfLine = a.Setting.EditorSetting.IsShowEOL;
        doc = a.Doc;
    }

    public override void ContextChange(string s)
    {
        _editor.Text = s;
    }

    private void TextChanged(object? sender, EventArgs e)
    {
        Markdown.Markdown = _editor.Text;
    }
    
    private void EditorKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftAlt)
        {
            var list = doc.Using.Select(x => x.ToUsing());
            CompletionWindow = new CompletionWindow(_editor.TextArea);
            CompletionWindow.Closed += (_, _) => CompletionWindow = null;
            var data = CompletionWindow.CompletionList.CompletionData;
            foreach (var item in list)
                data.Add(item);
        
            CompletionWindow.Show();
        }
    }

    #region ContextMenu
    
    public override void ShowTabs(object? sender, RoutedEventArgs e)
    {
        _editor.Options.ShowTabs = !_editor.Options.ShowTabs;
    }

    public override void ShowSpaces(object? sender, RoutedEventArgs e)
    {
        _editor.Options.ShowSpaces = !_editor.Options.ShowSpaces;
    }

    public override void ShowEOL(object? sender, RoutedEventArgs e)
    {
        _editor.Options.ShowEndOfLine = !_editor.Options.ShowEndOfLine;
    }

    #endregion

    public override void SearchMenuClick(object? sender, RoutedEventArgs e)
    {
        _editor.SearchPanel.Open();
        //Editor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new TextAreaDefaultInputHandler(Editor.TextArea));
    }

    public override async void CopyMenuClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        if(top == null)return;
        
        var text = _editor.SelectedText;
        if(string.IsNullOrEmpty(text))return;
        await top.Clipboard!.SetTextAsync(text);
    }

    public override async void PasteMenuClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        if(top == null)return;
        
        var text = await top.Clipboard!.GetTextAsync();
        
        _editor.Document.Text += text;
    }

    public override void CutMenuClick(object? sender, RoutedEventArgs e)
    {
        
    }

    public override string GetText()
    {
        return _editor.Text;
    }

    public void UsingKey()
    {
        var list = doc.Using.Select(x => x.ToUsing());
        CompletionWindow = new CompletionWindow(_editor.TextArea);
        CompletionWindow.Closed += (_, _) => CompletionWindow = null;
        var data = CompletionWindow.CompletionList.CompletionData;
        foreach (var item in list)
            data.Add(item);

        CompletionWindow.Show();
    }

    public void ParagraphKey()
    {
        var list = doc.Paragraph.Select(x => x.ToUsing());
        CompletionWindow = new CompletionWindow(_editor.TextArea);
        CompletionWindow.Closed += (_, _) => CompletionWindow = null;
        var data = CompletionWindow.CompletionList.CompletionData;
        foreach (var item in list)
            data.Add(item);

        CompletionWindow.Show();
    }

    public void FontKey()
    {
        var list = doc.Font.Select(x => x.ToUsing());
        CompletionWindow = new CompletionWindow(_editor.TextArea);
        CompletionWindow.Closed += (_, _) => CompletionWindow = null;
        var data = CompletionWindow.CompletionList.CompletionData;
        foreach (var item in list)
            data.Add(item);

        CompletionWindow.Show();
    }
}